package com.miss.spjyx

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import com.miss.spjyx.model.BaseRes
import com.miss.spjyx.model.DeviceModel
import com.miss.spjyx.network.CommonCallback
import com.miss.spjyx.network.Requester
import com.miss.spjyx.utils.UHfUtils
import com.miss.spjyx.widget.ProgressDialog
import com.uhf.scanlable.UHfData
import retrofit2.Call

/**
 * 设备识别
 *
 * @author wl
 * @since 2019/08/11 17:09
 */
class DeviceDetectFragment : Fragment() {
    lateinit var adapter: DeviceAdapter
    var uHfData: List<UHfData.InventoryTagMap> = arrayListOf()
    lateinit var tv_total: TextView
    var li = object : UHfUtils.onDataGotListener {
        override fun onDataChange(data: List<UHfData.InventoryTagMap>) {
            tv_total.text = "共${data.size}，已识别${adapter.data?.size ?: 0}"
            uHfData = data
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_device, container, false)

        val rv_data = view.findViewById<RecyclerView>(R.id.rv_data)
        adapter = DeviceAdapter(activity!!)
        rv_data.adapter = adapter
        tv_total = view.findViewById(R.id.tv_total)
        val btn_upload = view.findViewById<Button>(R.id.btn_upload)
        btn_upload.setOnClickListener { upload() }

        return view
    }

    override fun onResume() {
        super.onResume()
        if (!isHidden) {
            UHfUtils.register(activity!!)
            UHfUtils.li = li
        }
    }

    override fun onPause() {
        super.onPause()
        if (!isHidden) {
            UHfUtils.unregister(activity!!)
            UHfUtils.li = null
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden) {
            UHfUtils.unregister(activity!!)
            UHfUtils.li = null
        } else {
            UHfUtils.register(activity!!)
            UHfUtils.li = li
        }
    }

    fun upload() {
        if (uHfData.isEmpty()) {
            Toast.makeText(activity!!, "没有数据需要上传", Toast.LENGTH_SHORT).show()
            return
        }
        ProgressDialog.show(activity!!)
        val spms = ArrayList<String>()
        uHfData.forEach {
            spms.add(it.strEPC)
        }
        Requester.apiService()
            .deviceScan(spms.joinToString())
            .enqueue(object : CommonCallback<List<DeviceModel>>(activity!!) {
                override fun onData(data: List<DeviceModel>?) {
                    adapter.data = data
                    Toast.makeText(activity!!, "识别成功", Toast.LENGTH_SHORT).show()
                }

                override fun onDone() {
                    super.onDone()
                    ProgressDialog.dismiss()
                }
        })
    }
}